from rest_framework.viewsets import ModelViewSet
from meiduo_admin.utils import PageNum
from goods.models import SKU
from meiduo_admin.serializers.sku import SKUSerializer

class SKUModelViewSet(ModelViewSet):

    serializer_class = SKUSerializer

    pagination_class = PageNum

    def get_queryset(self):
        # 提取keyword
        keyword = self.request.query_params.get('keyword')

        if keyword == '' or keyword is None:
            return SKU.objects.all()
        else:
            return SKU.objects.filter(name=keyword)

from rest_framework.generics import ListAPIView
from goods.models import GoodsCategory
from meiduo_admin.serializers.sku import SKUCategorieSerializer

class SKUCategoriesView(ListAPIView):

    serializer_class = SKUCategorieSerializer
    # 根据数据存储规律parent_id大于37为三级分类信息，查询条件为parent_id__gt=37
    #queryset = GoodsCategory.objects.filter(parent_id__gt=37)
    #或者
    # 根据关联下级筛选最后一级数据：关联的下级为None，表示当前为最后一级
    queryset = GoodsCategory.objects.filter(goodscategory=None)


from rest_framework.generics import ListAPIView
from goods.models import Goods
from meiduo_admin.serializers.sku import GoodsSimpleSerializer

class GoodsSimpleView(ListAPIView):
    serializer_class = GoodsSimpleSerializer
    queryset = Goods.objects.all()

from rest_framework.generics import ListAPIView
from goods.models import GoodsSpecification
from meiduo_admin.serializers.sku import GoodsSpecSerialzier
class GoodsSpecView(ListAPIView):

    serializer_class = GoodsSpecSerialzier
    # 因为我们继承的是ListAPIView，在拓展类中是通过get_queryset获取数据，但是我们现在要获取的是规格信息，所以重写get_queryset
    def get_queryset(self):
        pk=self.kwargs['pk']
        return GoodsSpecification.objects.filter(spu_id=pk)


class SKUGoodsView(ModelViewSet):

    serializer_class = SKUSerializer

    pagination_class = PageNum

    def get_queryset(self):
        # 提取keyword
        keyword = self.request.query_params.get('keyword')

        if keyword == '' or keyword is None:
            return SKU.objects.all()
        else:
            return SKU.objects.filter(name=keyword)